<?php

require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorConfiguration.class.php';
require_once dirname(__FILE__) . '/../lib/ct_documentoGeneratorHelper.class.php';

/**
 * ct_documento actions.
 *
 * @package    ingenyo-biblio
 * @subpackage ct_documento
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ct_documentoActions extends autoCt_documentoActions
{

  public function executeIndex(sfWebRequest $request)
  {
    $this->quicksearch = $request->getParameter('quicksearch', false);
    parent::executeIndex($request);
    $this->filters = new CtDocumentoFormFilterCollection();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeDelete(sfWebRequest $request)
  {
    $this->redirect('ct_documento/index');

  }

  public function executeBuscar(sfWebRequest $request)
  {
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');

    if ((float) $this->getUser()->getAttribute('counter_search', 0) > (float) sfConfig::get('app_captcha_cantidad_intentos'))
    {
      $captcha = new securimage();
      if ($captcha->check($request->getParameter('captcha_code')))
      {
        $this->getUser()->setAttribute('counter_search', 0);
      }
      else
      {
        $captcha = $this->getUser()->getAttribute('captcha');
        return $this->renderText(get_partial('captcha', array('captcha' => $captcha, 'error' => true)));
      }
    }

    $form = new CtDocumentoFormFilterCollection();
    $form->bind($request->getParameter('filters'));
    $pager = new sfPropelPager('CTDocumento', sfConfig::get('app_max_documentos_en_busqueda'));
    $pager->setCriteria($form->getCriteriaFilter());
    $pager->setPage($request->getParameter('page', 1));

    $pager->init();

    $this->sort = $this->getSort();
    $this->helper = new ct_documentoGeneratorHelper();


    if ((float) $this->getUser()->getAttribute('counter_search', 0) == (float) sfConfig::get('app_captcha_cantidad_intentos'))
    {
      $captcha = mt_rand();
      $this->getUser()->setAttribute('captcha', $captcha);
    }
    else
    {
      $captcha = false;
    }

    $this->getUser()->setAttribute('counter_search', (float) $this->getUser()->getAttribute('counter_search', 0) + 1);
    $this->generarLog($request);
    
    return $this->renderText(get_partial('result_list', array('pager' => $pager,
          'sort' => $this->sort,
          'configuration' => $this->configuration,
          'helper' => $this->helper,
          'captcha' => $captcha)));

  }

  public function executeAddCustom(sfWebRequest $request)
  {
    $next = $request->getParameter('next');
    $is_the_last = $request->getParameter('is_the_last');
    $form = new CtDocumentoFormFilterCollection();
    $form->addCustom($next, $is_the_last);
    sfConfig::set('sf_web_debug', false);
    sfContext::getInstance()->getConfiguration()->loadHelpers('Partial');
    return $this->renderText(get_partial('custom', array('custom_filters' => $form['filters']['custom'][$next],
          'add' => true)));

  }

  public function executeShow(sfWebRequest $request)
  {
    $parametros = $request->getRequestParameters();
    $this->CtDocumento = $this->getRoute()->getObject();
    $this->caracteristicas = $this->CtDocumento->getCaracteristicasFicha();
    $this->caracteristicasAACR2 = $this->CtDocumento->getCaracteristicasFichaAACR2();
    $this->contenido = $this->CtDocumento->getContenido();
    if (array_key_exists('titulo_slug', $parametros))
    {
      $this->back_menu = true;
      $this->setLayout('layout');
    }
    else
    {
      $this->back_menu = false;
      $this->setLayout('layout_iframe');
    }

  }

  public function executeCaptchaImage(sfWebRequest $request)
  {
    $this->getUser()->setAttribute('captcha', $request->getParameter('sid'));
    $captcha = new securimage();
    $captcha->show('/captcha/backgrounds/bg3.jpg');

  }

  private function generarLog($request)
  {
    if (LogPeer::getTermino($request->getParameter('filters')) != ''){
    $log = new Log();
    $filter = $request->getParameter('filters');
    $log->setTipoBusqueda($filter['tipo_busqueda']);
    $log->setTermino(LogPeer::getTermino($request->getParameter('filters')));
    $log->setAplicacion('frontend');
    $log->save();
    }
  }
  
  public function executeGetTerminos(sfWebRequest $request){
    $params = $request->getParameter('filters');
    
    $json['terminosMasBuscado'] = LogPeer::geterminosMasBuscado(10);
    $json['personaQueBuscoEsteTermino'] = LogPeer::getPersonaQueBuscoEsteTerminoTambienBusco($params['valor_simple']);
    
    return $this->renderText(json_encode($json));
  }

}
